package com.sright.bcard.content.biz.service;

import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.biz.manager.TemplateManager;
import com.sright.bcard.content.dal.dao.TemplateDAO;
import com.sright.bcard.content.dal.entity.TemplateDO;
import com.sright.bcard.common.service.share.dto.content.CardTemplateDTO;
import com.sright.bcard.content.share.service.TemplateService;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.common.response.SrightResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: lumf
 * Date: 2020/8/17 3:50 PM
 */
@Component
@Slf4j
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    TemplateManager templateManager;

    @Autowired
    TemplateDAO templateDAO;

    @Override
    public Pager<CardTemplateDTO> searchTemplateByKeyword(String keyword, Integer pageIndex, Integer pageSize) {
        Integer count = templateDAO.count(keyword);
        Integer offset = (pageIndex - 1) * pageSize;
        PageParamRequest request = new PageParamRequest();
        request.setOffset(offset);
        request.setLimit(pageSize);
        List<TemplateDO> boList =  templateDAO.selectByName(keyword, request);
        List<CardTemplateDTO> dtoList = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(boList, dtoList, CardTemplateDTO.class);
        Pager<CardTemplateDTO> pager = new Pager<>(pageIndex, pageSize, count, dtoList);
        return pager;
    }

    @Override
    public SrightResponse templateList(TokenParam tokenParam) {
        return SrightResponse.defaultSuccess(templateManager.templateList(tokenParam));
    }

    @Override
    public SrightResponse selectTemplate(Long cardId, Long templateId) {
        return SrightResponse.defaultSuccess(templateManager.selectTemplate(cardId,templateId));
    }

    @Override
    public SrightResponse allTemplateList() {
        return SrightResponse.defaultSuccess(templateManager.allTemplateList());
    }

    @Override
    public SrightResponse templates() {
        return SrightResponse.defaultSuccess(templateManager.templates());
    }
}
